package com.yun.zf.admin.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfHouseCertificateMapper;
import com.yun.zf.admin.service.ZfHouseCertificateService;
import com.yun.zf.admin.service.ZfHouseDocumentInfoService;
import com.yun.zf.admin.service.ZfHouseScoreService;
import com.yun.zf.common.admin.dto.house.HouseCertificateDto;
import com.yun.zf.common.admin.model.house.DoucumentInfoMo;
import com.yun.zf.common.admin.model.house.HouseCertificateMo;
import com.yun.zf.common.app.domain.ZfHouseCertificate;
import com.yun.zf.common.app.domain.ZfHouseScore;
import com.yun.zf.common.mapstruct.admin.HouseMapper;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 房产证信息表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-05
 */
@Service
@Log
public class ZfHouseCertificateServiceImpl extends ServiceImpl<ZfHouseCertificateMapper, ZfHouseCertificate> implements ZfHouseCertificateService {

    @Autowired
    private ZfHouseDocumentInfoService documentInfoService;
    @Autowired
    private ZfHouseScoreService scoreService;

    /**
     * 上传房产证信息
     *
     * @param
     * @return
     */
    @Transactional
    @Override
    public int addHouseCertificate(HouseCertificateMo houseCertificateMo, Long provinceCode, Long cityCode) {
        // 1、保存房产证信息
        ZfHouseCertificate houseCertificate = HouseMapper.INSTANCE.houseCertMoToCert(houseCertificateMo);
        houseCertificate.setCityCode(cityCode);
        houseCertificate.setProvinceCode(provinceCode);
        int cn = baseMapper.insert(houseCertificate);
        if (cn>0){
            calculateScore(Long.parseLong(houseCertificateMo.getBasicId()),cityCode,provinceCode);
        }

        return cn;
    }

    /**
     * 编辑房产证信息
     *
     * @param houseCertificateMo
     * @return
     */
    @Transactional
    @Override
    public int motifyHouseCertificate(HouseCertificateMo houseCertificateMo, Long provinceCode, Long cityCode) {
        // 1、保存房产证信息
        ZfHouseCertificate houseCertificate = HouseMapper.INSTANCE.houseCertMoToCert(houseCertificateMo);
        houseCertificate.setCityCode(cityCode);
        houseCertificate.setProvinceCode(provinceCode);
        QueryWrapper<ZfHouseCertificate> certificateQueryWrapper = new QueryWrapper<>();
        certificateQueryWrapper.lambda().eq(ZfHouseCertificate::getId, houseCertificate.getId())
                .eq(ZfHouseCertificate::getCityCode, houseCertificate.getCityCode())
                .eq(ZfHouseCertificate::getProvinceCode, houseCertificate.getProvinceCode());
        ZfHouseCertificate c = baseMapper.selectOne(certificateQueryWrapper);
        BeanUtil.copyProperties(houseCertificate, c, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        // 修改证件数据
        int count = baseMapper.update(c, certificateQueryWrapper);
        return count;
    }


    /**
     * 查询产证信息
     *
     * @param basicId
     * @param provinceCode
     * @param cityCode
     * @return
     */
    @Override
    public HouseCertificateDto getHouseCertificate(Long basicId, Long provinceCode, Long cityCode) {
        // 获取产证信息
        QueryWrapper<ZfHouseCertificate> certificateQueryWrapper = new QueryWrapper<>();
        certificateQueryWrapper.lambda().eq(ZfHouseCertificate::getCityCode, cityCode)
                .eq(ZfHouseCertificate::getProvinceCode, provinceCode).eq(ZfHouseCertificate::getBasicId, basicId)
                .orderByDesc(ZfHouseCertificate::getModifiedTime);
       List<ZfHouseCertificate> list =  baseMapper.selectList(certificateQueryWrapper);
        ZfHouseCertificate houseCertificate = list.size()>0?list.get(0):null;
        // 获取证件图片
        List<DoucumentInfoMo> infoMoList = documentInfoService.getDocumentInfo(basicId, provinceCode, cityCode, 1);

        // 配置返回信息
        HouseCertificateMo houseCertificateMo = HouseMapper.INSTANCE.houseCertToCertMo(houseCertificate);
        HouseCertificateDto certificateDto = new HouseCertificateDto();
        certificateDto.setCertificateMo(houseCertificateMo);
        certificateDto.setDocumentInfoMoList(infoMoList);

        return certificateDto;
    }

    /**
     *
     * @param basicId
     * @param cityCode
     * @param provCode
     */
    public void calculateScore( Long basicId, Long cityCode, Long provCode) {
        // 电梯权重占30%， 楼层占70%(其中底层10%，低层20%,中楼层25%，高楼层30%，顶层15% )
        ZfHouseScore houseScore = new ZfHouseScore();
        houseScore.setCzScore(new BigDecimal("10"));
        houseScore.setCz(new BigDecimal("100"));
        houseScore.setBasicId(basicId);
        houseScore.setCityCode(cityCode);
        houseScore.setProvinceCode(provCode);
        // 更新房源分数
        scoreService.addOrUpdate(houseScore);

    }
}
